var loadData;
require(['highcharts'],function () {
    var $dataChart=$("#dataChart");
    //图表初始化
    function initChart(){
        $dataChart.highcharts({title: {
            text: '正在查询中，请稍后'
        },
            credits: {
                enabled: false
            }});
    }
    //图表显示空数据
    function chartWithoutData(){
        $dataChart.highcharts({
            title: {
                text: '无数据'
            }, credits: {
                enabled: false
            }
        });
    }
    //图表显示数据
    function chartWithData(title,text1,text2,categories,data1,data2) {
        $dataChart.highcharts({
            chart: {
                zoomType: 'x',
                spacingRight: 20,
                width: 750
            },
            title: {
                text: title
            },
            xAxis: {
                categories: categories
            },
            yAxis: [{
                labels: {

                    style: {
                        color: '#89A54E'
                    }
                },
                title: {
                    text: text2,
                    style: {
                        color: '#89A54E'
                    }
                },
                //min:0, // 定义最小值
                opposite: true
            }, {
                gridLineWidth: 0,
                title: {
                    text: text1,
                    style: {
                        color: '#4572A7'
                    }
                },
                //min:0, // 定义最小值
                labels: {
                    style: {
                        color: '#4572A7'
                    }
                }
            }],
            credits: {
                enabled: false
            },
            tooltip: {
                shared: true
            },
            legend: {
                enabled: false
            },
            plotOptions: {
                area: {
                    color: '#489648',
                    fillColor: {
                        linearGradient: {x1: 0, y1: 0, x2: 0, y2: 1},
                        stops: [
                            [0, Highcharts.getOptions().colors['#489648']],
                            [1, Highcharts.Color(Highcharts.getOptions().colors['#489648']).setOpacity(0).get('rgba')]
                        ]
                    },
                    lineWidth: 1,
                    marker: {
                        enabled: false
                    },
                    shadow: false,
                    states: {
                        hover: {
                            lineWidth: 1
                        }
                    },
                    threshold: null
                }
            },

            series: [{
                name: text1,
                color: '#4572A7',
                type: 'spline',
                yAxis: 1,
                data: data1
            }, {
                name: text2,
                color: '#89A54E',
                type: 'spline',
                data: data2
            }]
        });
    }
    //加载数据
    loadData=function(type,reportDate,dayOnDay,dayOnWeak,data) {
        initChart();
        if(type=='bhv'){
            $.ajaxExt({
                url : "/qualityAnlsSummary/getLast8TBsStatWeakBhvList.htm",
                data: {"corpId":data.corpId,"organId":data.organId,"lineId":data.lineId,"bhvType":data.bhvType,"reportDate":reportDate,"dayOnDay":dayOnDay,"dayOnWeak":dayOnWeak}
            }).done(function(result) {
                console.info(result);
                if (result) {
                    var categories = [];
                    var data1 = [];
                    var data2 = [];
                    $.each(result,function(i){
                        categories.push(result[i].reportDate);
                        data1.push(result[i].occurCount);
                        data2.push(result[i].dayOnDay);
                    });
                    chartWithData(data.bhvTypeName,'数量','增长率',categories,data1,data2);
                }
                else{
                    chartWithoutData();
                }
            });
        }
        else if(type=='mft'){
            $.ajaxExt({
                url : "/qualityAnlsSummary/getLast8TBsStatWeakMftList.htm",
                data: {"corpId":data.corpId,"organId":data.organId,"lineId":data.lineId,"mftDiscCode":data.mftDiscCode,"mftTypeCode":data.mftTypeCode,"reportDate":reportDate,"dayOnDay":dayOnDay,"dayOnWeak":dayOnWeak}
            }).done(function(result) {
                console.info(result);
                if (result) {
                    var categories = [];
                    var data1 = [];
                    var data2 = [];
                    $.each(result,function(i){
                        categories.push(result[i].reportDate);
                        data1.push(result[i].occurCount);
                        data2.push(result[i].dayOnDay);
                    });
                    chartWithData(data.mftTypeName,'数量','增长率',categories,data1,data2);
                }
                else{
                    chartWithoutData();
                }
            });
        }
        else if(type=='online'){
            $.ajaxExt({
                url : "/qualityAnlsSummary/getLast8TBsStatWeakOnlineList.htm",
                data: {"corpId":data.corpId,"organId":data.organId,"lineId":data.lineId,"reportDate":reportDate,"dayOnDay":dayOnDay,"dayOnWeak":dayOnWeak}
            }).done(function(result) {
                if (result) {
                    var categories = [];
                    var data1 = [];
                    var data2 = [];
                    $.each(result,function(i){
                        categories.push(result[i].reportDate);
                        data1.push(result[i].baseLineValue);
                        data2.push(result[i].onlineRate);
                    });
                    chartWithData('在线率','基准线','在线率',categories,data1,data2);
                }
                else{
                    chartWithoutData();
                }
            });
        }
    }
});